package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.Entity;
import com.zw.yzk.kabu.data.network.entity.GoodsOrderDetailsEntity;
import com.zw.yzk.kabu.data.network.entity.PayEntity;
import com.zw.yzk.kabu.data.network.entity.QueryOrderStateEntity;
import com.zw.yzk.kabu.data.network.task.ConfirmReceiveTask;
import com.zw.yzk.kabu.data.network.task.GoodsOrderDetailsTask;
import com.zw.yzk.kabu.data.network.task.PayTask;
import com.zw.yzk.kabu.data.network.task.QueryOrderStateTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.mapper.GoodsOrderDetailsMapper;
import com.zw.yzk.kabu.utils.WechatPayInfo;
import com.zw.yzk.kabu.view.view.GoodsOrderDetailsView;
import com.zw.yzk.kabu.wxapi.WechatManager;

import java.util.ArrayList;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/14.
 */

public class GoodsOrderDetailsPresenter implements Presenter {

    private UseCase getGoodsOrderDetails;
    private UseCase pay;
    private UseCase confirmReceive;
    private UseCase checkOrderState;
    private AppCompatActivity activity;
    private GoodsOrderDetailsMapper mapper;
    private GoodsOrderDetailsView orderView;
    private String tradeId;

    @Inject
    public GoodsOrderDetailsPresenter(@Named("goodsOrderDetailsUseCase") UseCase getGoodsOrderDetails,
                                      @Named("payUseCase") UseCase pay,
                                      @Named("confirmReceiveUseCase") UseCase confirmReceive,
                                      @Named("queryOrderUseCase") UseCase checkOrderState,
                                      GoodsOrderDetailsMapper mapper,
                                      AppCompatActivity activity) {
        this.getGoodsOrderDetails = getGoodsOrderDetails;
        this.confirmReceive = confirmReceive;
        this.checkOrderState = checkOrderState;
        this.pay = pay;
        this.mapper = mapper;
        this.activity = activity;
    }

    public void setView(GoodsOrderDetailsView view) {
        this.orderView = view;
    }

    public void getGoodsOrderDetails(String orderId, int type) {
        getGoodsOrderDetails.execute(new GoodsOrderDetailsSubscriber(activity), new GoodsOrderDetailsTask(orderId, type));
    }

    public void pay(String orderId) {
        PayTask task = new PayTask();
        task.orderList = new ArrayList<>();
        task.orderList.add(orderId);
        pay.execute(new PaySubscriber(activity), task);
    }

    public void confirmReceive(String orderId) {
        confirmReceive.execute(new ConfirmSubscriber(activity), new ConfirmReceiveTask(orderId));
    }

    public void checkOrderState() {
        if (TextUtils.isEmpty(tradeId)) {
            return;
        }
        checkOrderState.execute(new QueryOrderStateSubscriber(activity), new QueryOrderStateTask(tradeId));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getGoodsOrderDetails.unSubscribe();
        pay.unSubscribe();
        confirmReceive.unSubscribe();
        checkOrderState.unSubscribe();
    }

    private class QueryOrderStateSubscriber extends BaseSubscriber<QueryOrderStateEntity> {

        public QueryOrderStateSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public void onNext(QueryOrderStateEntity entity) {
            super.onNext(entity);
            if (entity.result.result == 1) {
                orderView.queryOrderStateSuccess();
            }
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.check_order_state_by_yourself);
        }
    }

    private class GoodsOrderDetailsSubscriber extends BaseSubscriber<GoodsOrderDetailsEntity> {

        public GoodsOrderDetailsSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(GoodsOrderDetailsEntity goodsOrderDetailsEntity) {
            super.onNext(goodsOrderDetailsEntity);
            orderView.getGoodsOrderDetailsSuccess(mapper.transfer(goodsOrderDetailsEntity));
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_goods_order_details_failed);
        }
    }

    private class PaySubscriber extends BaseSubscriber<PayEntity> {

        public PaySubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(PayEntity entity) {
            super.onNext(entity);
            //设置支付信息
            tradeId = entity.result.tradeNo;
            //调起微信支付
            WechatManager.payByWeChat(activity, entity.result.wechatPay.appId, entity.result.wechatPay.partnerId, entity.result.wechatPay.prepayId,
                    entity.result.wechatPay.packageName, entity.result.wechatPay.nonceStr, entity.result.wechatPay.timeStamp, entity.result.wechatPay.sign);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.submit_order_failed);
        }
    }

    private class ConfirmSubscriber extends BaseSubscriber<Entity> {

        public ConfirmSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(Entity entity) {
            super.onNext(entity);
            orderView.receiveSuccess();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.confirm_receive_failed);
        }
    }
}
